//
//  VC6PropertyAnimation.swift
//  ChapterAll
//
//  Created by Kevin on 2020/4/27.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VC6PropertyAnimation: UIViewController {
    
    lazy var redView = { ()-> UIView in
        let ui = UIView()
        ui.backgroundColor = UIColor.red
        ui.center = CGPoint(x: view.center.x - 50, y: view.center.y - 50)
        ui.frame.size = CGSize(width: 100, height: 100)
        ui.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickRedView)))
        return ui
    }()
    
    lazy var redBGView = { ()-> RedBGView in
        let ui = RedBGView(frame: view.frame)
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        redBGView.addSubview(redView)
        view.addSubview(redBGView)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        test5()
    }
    
    func test5() -> Void {
        let path = UIBezierPath()
        path.addArc(withCenter: view.center, radius: 150, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: false)
        
        let keyAnimation = CAKeyframeAnimation(keyPath: "position")
        keyAnimation.path = path.cgPath
        
        let basicAnimation = CABasicAnimation(keyPath: "transform.rotation")
        basicAnimation.toValue = CGFloat(Double.pi * 2) * 5
        
        let group = CAAnimationGroup()
        group.duration = 3
        group.repeatCount = MAXFLOAT
        group.animations = [keyAnimation,basicAnimation]
        redView.layer.add(group, forKey: nil)
    }
    
    func test4() -> Void {
        let path = UIBezierPath()
        path.addArc(withCenter: view.center, radius: 150, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: false)
        
        let animation = CAKeyframeAnimation(keyPath: "position")
        animation.duration = 3
        animation.path = path.cgPath
        animation.repeatCount = MAXFLOAT
        redView.layer.add(animation, forKey: nil)
    }
    
    func test3() -> Void {
        let animation = CABasicAnimation(keyPath: "position.y")
        animation.byValue = 20
        // 不回到原始位置
        animation.fillMode = .forwards
        animation.isRemovedOnCompletion = false
        redView.layer.add(animation, forKey: nil)
    }
    
    func test2() -> Void {
        let animation = CABasicAnimation(keyPath: "position.y")
        animation.toValue = 400
        // 不回到原始位置
        animation.fillMode = .forwards
        animation.isRemovedOnCompletion = false
        redView.layer.add(animation, forKey: nil)
    }
    
    func test1() -> Void {
        let animation = CABasicAnimation(keyPath: "position.y")
        animation.toValue = 400
        redView.layer.add(animation, forKey: nil)
    }
    
    @objc func clickRedView() -> Void {
        print("red view was clicked")
    }
}

class RedBGView: UIView {
    
    override func draw(_ rect: CGRect) {
        let path = UIBezierPath()
        path.addArc(withCenter: center, radius: 150, startAngle: 0, endAngle: CGFloat(Double.pi * 2), clockwise: false)
        UIColor.black.setStroke()
        path.stroke()
    }
}
